package com.hyk.commonLib.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;

import androidx.annotation.ColorInt;
import androidx.appcompat.widget.AppCompatImageView;

import com.hyk.commonLib.R;
import com.hyk.commonLib.common.utils.ScreenUtils;

/**
 * Created by 贺玉琨 on 2019/10/14.
 */
public class RoundCornerImageView extends AppCompatImageView {

    protected float cornerRadiusLT;
    protected float cornerRadiusRT;
    protected float cornerRadiusLB;
    protected float cornerRadiusRB;
    @ColorInt
    private int borderColor;
    private int borderWidth;
    private boolean showBorder;
    private Path mPath;
    private RectF mRectF;
    /**
     * 利用clip剪切的四个角半径，八个数据分别代表左上角（x轴半径，y轴半径），右上角（**），右下角（**），左下角（**）
     */
    private float[] rids = new float[8];
    private PaintFlagsDrawFilter paintFlagsDrawFilter;
    Paint paint;

    public RoundCornerImageView(Context context) {
        this(context, null);
    }

    public RoundCornerImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundCornerImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttr(attrs);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    private void initAttr(AttributeSet attrs) {
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.RoundCornerImageView);
        float cornerRadius = a.getDimension(R.styleable.RoundCornerImageView_cornerRadius, ScreenUtils.dp2px(10, this));
        cornerRadiusLT = a.getDimension(R.styleable.RoundCornerImageView_cornerRadiusLT, cornerRadius);
        cornerRadiusRT = a.getDimension(R.styleable.RoundCornerImageView_cornerRadiusRT, cornerRadius);
        cornerRadiusLB = a.getDimension(R.styleable.RoundCornerImageView_cornerRadiusLB, cornerRadius);
        cornerRadiusRB = a.getDimension(R.styleable.RoundCornerImageView_cornerRadiusRB, cornerRadius);
        borderWidth = (int) a.getDimension(R.styleable.RoundCornerImageView_borderWidth, 0);
        borderColor = a.getColor(R.styleable.RoundCornerImageView_borderColor, Color.TRANSPARENT);
        showBorder = a.getBoolean(R.styleable.RoundCornerImageView_showBorder, true);
        a.recycle();
    }

    public void setRadius(int radius) {
        setRadius(radius, true);
    }

    public void setRadius(int radius, boolean isDp) {
        float cornerRadius = isDp ? ScreenUtils.dp2px(radius) : radius;
        cornerRadiusLT = cornerRadius;
        cornerRadiusRT = cornerRadius;
        cornerRadiusLB = cornerRadius;
        cornerRadiusRB = cornerRadius;
        refreshCornerInfo();
        invalidate();
    }

    protected void refreshCornerInfo() {
        rids = new float[]{
                cornerRadiusLT, cornerRadiusLT,
                cornerRadiusRT, cornerRadiusRT,
                cornerRadiusRB, cornerRadiusRB,
                cornerRadiusLB, cornerRadiusLB,
        };
        mPath = new Path();
        paintFlagsDrawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        //这里的目的是将画布设置成一个顶部边缘是圆角的矩形
//        if (width > mRadiusPx && height > mRadiusPx) {
//            path.reset();
//
//            path.moveTo(mRadiusPx, 0);
//            path.lineTo(width - mRadiusPx, 0);
//            path.quadTo(width, 0, width, mRadiusPx);
//            path.lineTo(width, height - mRadiusPx);
//            path.quadTo(width, height, width - mRadiusPx, height);
//            path.lineTo(mRadiusPx, height);
//            path.quadTo(0, height, 0, height - mRadiusPx);
//            path.lineTo(0, mRadiusPx);
//            path.quadTo(0, 0, mRadiusPx, 0);
//
//            canvas.clipPath(path);
//        }

        if (mPath == null) {
            refreshCornerInfo();
        }

        if (mRectF == null) {
            super.onDraw(canvas);
            return;
        }

        mPath.reset();
        mPath.addRoundRect(mRectF, rids, Path.Direction.CW);
        canvas.setDrawFilter(paintFlagsDrawFilter);
        canvas.save();
        canvas.clipPath(mPath);
        super.onDraw(canvas);
        //绘制边框
        if (showBorder && borderWidth > 0) {
            paint.setColor(borderColor);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(borderWidth * 2);
            canvas.drawPath(mPath, paint);
        }
        canvas.restore();
    }

    public void setBorderWidthInDp(int width) {
        borderWidth = ScreenUtils.dp2px(width);
        invalidate();
    }

    public void setBorderWidthColor(@ColorInt int color) {
        borderColor = color;
        invalidate();
    }

    public void setShowBorder(boolean showBorder) {
        this.showBorder = showBorder;
        invalidate();
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mRectF = new RectF(0, 0, w, h);
    }
}